/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * matStep2Mall.java
 *
 * Created on 2011-feb-25, 09:57:17
 */

package imat;

import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import se.chalmers.ait.dat215.project.IMatDataHandler;
import se.chalmers.ait.dat215.project.Product;
import se.chalmers.ait.dat215.project.ShoppingCart;
import se.chalmers.ait.dat215.project.ShoppingItem;

/**
 *
 * @author David
 */
public class matStep3Mall extends javax.swing.JPanel {

    private IMatDataHandler handler = IMatDataHandler.getInstance();
    private ImageIcon star = new ImageIcon("src/imat/resources/images/buttons/star.gif");
    private ImageIcon star2 = new ImageIcon("src/imat/resources/images/buttons/star2.gif");
    private Product product;
    private double price;
    private ShoppingCart cart = IMatDataHandler.getInstance().getShoppingCart();
    private IMatPresenter presenter = IMatPresenter.getInstance();

    /** Creates new form matStep2Mall */
    public matStep3Mall() {
        initComponents();
    }

    public void showProduct(Product product) {
        detailPriceLabel.setText(presenter.doublePad(product.getPrice()));
        detailTotalCostLabel.setText(presenter.doublePad(product.getPrice()) + " kr");
        detailUnitLabel.setText(product.getUnitSuffix());
        detailUnitLabel2.setText(product.getUnit());
        detailDescriptionTextPane.setText(product.getName() + " är gott!");
        detailPictureLabel.setIcon(new ImageIcon(new ImageIcon("src/imat/resources/imat/images/" + product.getImageName()).getImage().getScaledInstance(detailPictureLabel.getWidth() - 10, detailPictureLabel.getHeight() - 10, Image.SCALE_SMOOTH)));
        detailProductNameLabel.setText(product.getName());
        price        = product.getPrice();
        this.product = product;

        if(IMatDataHandler.getInstance().isFavorite(product))
        {
            detailFavoriteButton.setIcon(star);
        }

        updateDetailBasketAmountLabel();
    }

    private void updateDetailBasketAmountLabel() {
        int amount = 0;

        for(ShoppingItem sItem : cart.getItems()) {
            if(sItem.getProduct().equals(product)) {
                amount = ((Double)sItem.getAmount()).intValue();
                break;
            }
        }

        detailBasketAmountLabel.setText("Du har " + amount + " " + product.getUnitSuffix() + " i kundkorgen");
    }

    public JLabel getDetailCostLabel() {
        return detailCostLabel;
    }

    public JTextPane getDetailDescriptionTextPanel() {
        return detailDescriptionTextPane;
    }

    public JLabel getDetailPicturePanel() {
        return detailPictureLabel;
    }

    public JLabel getDetailPriceLabel() {
        return detailPriceLabel;
    }

    public JLabel getDetailProductNameLabel() {
        return detailProductNameLabel;
    }

        public void updateFavourite()
    {
        if(handler.isFavorite(product))
        {
            handler.removeFavorite(product);
            detailFavoriteButton.setIcon(star2);
        }
        else
        {
            handler.addFavorite(product);
            detailFavoriteButton.setIcon(star);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        detailPanel = new javax.swing.JPanel();
        detailPictureLabel = new javax.swing.JLabel();
        detailScrollPanel = new javax.swing.JScrollPane();
        detailDescriptionTextPane = new javax.swing.JTextPane();
        detailProductNameLabel = new javax.swing.JLabel();
        detailFavoriteButton = new javax.swing.JButton();
        detailPriceLabel = new javax.swing.JLabel();
        SpinnerModel model = new SpinnerNumberModel(1, 1, 1000, 1);
        detailAmountSpinner = new javax.swing.JSpinner(model);
        detailAmountLabel = new javax.swing.JLabel();
        detailUnitLabel = new javax.swing.JLabel();
        detailCostLabel = new javax.swing.JLabel();
        detailBasketButton = new javax.swing.JButton();
        detailFavoriteLabel = new javax.swing.JLabel();
        detailBasketAmountLabel = new javax.swing.JLabel();
        detailUnitLabel2 = new javax.swing.JLabel();
        detailTotalCostLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(795, 784));

        detailPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        detailPanel.setName("detailPanel"); // NOI18N
        detailPanel.setOpaque(false);
        detailPanel.setPreferredSize(new java.awt.Dimension(775, 209));

        detailPictureLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(imat.IMatApp.class).getContext().getResourceMap(matStep3Mall.class);
        detailPictureLabel.setText(resourceMap.getString("detailPictureLabel.text")); // NOI18N
        detailPictureLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        detailPictureLabel.setName("detailPictureLabel"); // NOI18N

        detailScrollPanel.setName("detailScrollPanel"); // NOI18N

        detailDescriptionTextPane.setText(resourceMap.getString("detailDescriptionTextPane.text")); // NOI18N
        detailDescriptionTextPane.setName("detailDescriptionTextPane"); // NOI18N
        detailScrollPanel.setViewportView(detailDescriptionTextPane);

        detailProductNameLabel.setFont(resourceMap.getFont("detailProductNameLabel.font")); // NOI18N
        detailProductNameLabel.setText(resourceMap.getString("detailProductNameLabel.text")); // NOI18N
        detailProductNameLabel.setName("detailProductNameLabel"); // NOI18N

        detailFavoriteButton.setIcon(resourceMap.getIcon("detailFavoriteButton.icon")); // NOI18N
        detailFavoriteButton.setText(resourceMap.getString("detailFavoriteButton.text")); // NOI18N
        detailFavoriteButton.setToolTipText(resourceMap.getString("detailFavoriteButton.toolTipText")); // NOI18N
        detailFavoriteButton.setName("detailFavoriteButton"); // NOI18N
        detailFavoriteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detailFavoriteButtonActionPerformed(evt);
            }
        });

        detailPriceLabel.setFont(resourceMap.getFont("detailTotalCostLabel.font")); // NOI18N
        detailPriceLabel.setText(resourceMap.getString("detailPriceLabel.text")); // NOI18N
        detailPriceLabel.setName("detailPriceLabel"); // NOI18N

        detailAmountSpinner.setName("detailAmountSpinner"); // NOI18N
        detailAmountSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                detailAmountSpinnerStateChanged(evt);
            }
        });

        detailAmountLabel.setFont(resourceMap.getFont("detailTotalCostLabel.font")); // NOI18N
        detailAmountLabel.setText(resourceMap.getString("detailAmountLabel.text")); // NOI18N
        detailAmountLabel.setName("detailAmountLabel"); // NOI18N

        detailUnitLabel.setFont(resourceMap.getFont("detailTotalCostLabel.font")); // NOI18N
        detailUnitLabel.setText(resourceMap.getString("detailUnitLabel.text")); // NOI18N
        detailUnitLabel.setName("detailUnitLabel"); // NOI18N

        detailCostLabel.setFont(resourceMap.getFont("detailTotalCostLabel.font")); // NOI18N
        detailCostLabel.setText(resourceMap.getString("detailCostLabel.text")); // NOI18N
        detailCostLabel.setName("detailCostLabel"); // NOI18N

        detailBasketButton.setText(resourceMap.getString("detailBasketButton.text")); // NOI18N
        detailBasketButton.setToolTipText(resourceMap.getString("detailBasketButton.toolTipText")); // NOI18N
        detailBasketButton.setName("detailBasketButton"); // NOI18N
        detailBasketButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detailBasketButtonActionPerformed(evt);
            }
        });

        detailFavoriteLabel.setFont(resourceMap.getFont("detailFavoriteLabel.font")); // NOI18N
        detailFavoriteLabel.setText(resourceMap.getString("detailFavoriteLabel.text")); // NOI18N
        detailFavoriteLabel.setName("detailFavoriteLabel"); // NOI18N

        detailBasketAmountLabel.setFont(resourceMap.getFont("detailBasketAmountLabel.font")); // NOI18N
        detailBasketAmountLabel.setText(resourceMap.getString("detailBasketAmountLabel.text")); // NOI18N
        detailBasketAmountLabel.setName("detailBasketAmountLabel"); // NOI18N

        detailUnitLabel2.setFont(resourceMap.getFont("detailTotalCostLabel.font")); // NOI18N
        detailUnitLabel2.setText(resourceMap.getString("detailUnitLabel2.text")); // NOI18N
        detailUnitLabel2.setName("detailUnitLabel2"); // NOI18N

        detailTotalCostLabel.setFont(resourceMap.getFont("detailTotalCostLabel.font")); // NOI18N
        detailTotalCostLabel.setText(resourceMap.getString("detailTotalCostLabel.text")); // NOI18N
        detailTotalCostLabel.setName("detailTotalCostLabel"); // NOI18N

        javax.swing.GroupLayout detailPanelLayout = new javax.swing.GroupLayout(detailPanel);
        detailPanel.setLayout(detailPanelLayout);
        detailPanelLayout.setHorizontalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(detailBasketAmountLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(detailPictureLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(detailProductNameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, detailPanelLayout.createSequentialGroup()
                        .addComponent(detailPriceLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(detailUnitLabel2)))
                .addGap(21, 21, 21)
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(detailScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
                    .addGroup(detailPanelLayout.createSequentialGroup()
                        .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, detailPanelLayout.createSequentialGroup()
                                .addComponent(detailFavoriteLabel)
                                .addGap(18, 18, 18)
                                .addComponent(detailFavoriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(detailPanelLayout.createSequentialGroup()
                                .addComponent(detailAmountLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(detailAmountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(detailUnitLabel))
                            .addGroup(detailPanelLayout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(detailTotalCostLabel))
                            .addComponent(detailCostLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                        .addComponent(detailBasketButton)))
                .addContainerGap())
        );
        detailPanelLayout.setVerticalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(detailFavoriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(detailPanelLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(detailFavoriteLabel)
                            .addComponent(detailProductNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(detailScrollPanel)
                    .addComponent(detailPictureLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(detailPanelLayout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(detailBasketAmountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(detailAmountLabel)
                            .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(detailPriceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(detailUnitLabel2))))
                    .addGroup(detailPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(detailAmountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(detailPanelLayout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(detailUnitLabel)
                            .addComponent(detailBasketButton, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(detailTotalCostLabel)
                    .addComponent(detailCostLabel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(detailPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(detailPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void detailBasketButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detailBasketButtonActionPerformed
        presenter.addToCart(product, (Integer) detailAmountSpinner.getValue());
        updateDetailBasketAmountLabel();
    }//GEN-LAST:event_detailBasketButtonActionPerformed

    private void detailFavoriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detailFavoriteButtonActionPerformed
        updateFavourite();
    }//GEN-LAST:event_detailFavoriteButtonActionPerformed

    private void detailAmountSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_detailAmountSpinnerStateChanged
        detailTotalCostLabel.setText(presenter.doublePad(price * (Integer)detailAmountSpinner.getValue()) + " kr");
    }//GEN-LAST:event_detailAmountSpinnerStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel detailAmountLabel;
    private javax.swing.JSpinner detailAmountSpinner;
    private javax.swing.JLabel detailBasketAmountLabel;
    private javax.swing.JButton detailBasketButton;
    private javax.swing.JLabel detailCostLabel;
    private javax.swing.JTextPane detailDescriptionTextPane;
    private javax.swing.JButton detailFavoriteButton;
    private javax.swing.JLabel detailFavoriteLabel;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JLabel detailPictureLabel;
    private javax.swing.JLabel detailPriceLabel;
    private javax.swing.JLabel detailProductNameLabel;
    private javax.swing.JScrollPane detailScrollPanel;
    private javax.swing.JLabel detailTotalCostLabel;
    private javax.swing.JLabel detailUnitLabel;
    private javax.swing.JLabel detailUnitLabel2;
    // End of variables declaration//GEN-END:variables

}
